package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.LiveTvStationDetail;
import com.elinkway.infinitemovies.bean.PlayData;
import com.elinkway.infinitemovies.ui.activity.play.PlayActivityLive;
import com.elinkway.infinitemovies.utils.StringUtil;
import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;

import java.util.ArrayList;

public class RecommendLiveVideoListAdapter extends MyBaseAdapter {
	private ArrayList<LiveTvStationDetail> mChannels;
	private Context mContext;

	public RecommendLiveVideoListAdapter(Context context,ArrayList<LiveTvStationDetail> data) {
		super(context);
		mChannels = data;
		mContext = context;
	}

	@Override
	public int getCount() {
		return mChannels.size();
	}

	@Override
	public LiveTvStationDetail getItem(int position) {
		return mChannels.get(position);
	}

	private class ItemHolder {

		TextView channelName;
		TextView videoName;
		RelativeLayout parent;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemHolder itemHolder = null;
		final LiveTvStationDetail detail = mChannels.get(position);
		if (null == convertView) {
			convertView = inflater.inflate(R.layout.recommend_live_listitem,parent, false);
			itemHolder = new ItemHolder();

			itemHolder.channelName = (TextView) convertView.findViewById(R.id.tv_name);
			itemHolder.videoName = (TextView) convertView.findViewById(R.id.show_name);
			itemHolder.parent = (RelativeLayout) convertView.findViewById(R.id.parent);

			convertView.setTag(itemHolder);
		} else {
			itemHolder = (ItemHolder) convertView.getTag();
		}
		if (StringUtil.isEmpty(detail.getPlayTimeList().get(0).getTitle())) {
			itemHolder.channelName.setText(detail.getName());
		} else {
			itemHolder.channelName.setText(detail.getName() + ":");
		}
		itemHolder.videoName.setText(detail.getPlayTimeList().get(0).getTitle());
		itemHolder.parent.setOnClickListener(new OnFeatureThematicClickListener(mContext, position));
		return convertView;
	}

	private class OnFeatureThematicClickListener implements OnClickListener {
		private Context mContext;
		private int position;

		public OnFeatureThematicClickListener(Context context, int position) {
			mContext = context;
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			PlayData playData = new PlayData();
			playData.setLiveStreams(mChannels.get(position).getStreams());
			playData.setmViewName(mChannels.get(position).getName());
			Bundle bundle = new Bundle();
			bundle.putSerializable(Utils.PLAY_DATA, playData);
			Intent intent = new Intent();
			intent.setClass(mContext, PlayActivityLive.class);
			intent.putExtras(bundle);
			mContext.startActivity(intent);
		}

	}
}
